<template>
  <el-checkbox-group v-model="currentValue" v-bind="$attrs" v-on="$listeners" @change="changeValue">
    <slot />
  </el-checkbox-group>
</template>

<script>
export default {
  name: 'ZmCheckboxGroup',
  props: {
    value: [String, Number, Array],
    str: {
      type: Boolean,
      default: true
    }, // 是否string
    rangeSeparator: {
      type: String,
      default: ','
    }

  },

  data () {
    return {
    }
  },

  computed: {
    currentValue: {
      set (v) {
        this.$nextTick(() => {
          if (this.str) {
            this.$emit('input', v.join(this.rangeSeparator))
          } else {
            this.$emit('input', v)
          }
        })
      },
      get () {
        let value = this.value || ''
        if (value instanceof Array) {
          return value
        }
        return (value + '').split(this.rangeSeparator).filter(Boolean)
      }
    }
  },
  methods: {
    // 初始化方法
    init () { },
    changeValue (val) {
      if (this.str) {
        this.$emit('on-change', val.join(this.rangeSeparator))
      } else {
        this.$emit('on-change', val)
      }
    }
  }
}
</script>
